// pages/dacmode/roomlist.js
var acid,roommsg='',userinfo = '',openid,romid='',sitesite
Page({
  data: {
    message1:[
      {  id:0,  img:'../image/dt/dianzan.png',  name:'yy',  biaozhi:'组员',  show:true },
      {  id:1,  img:'../image/dt/dianzan.png',  name:'hh',  biaozhi:'组员',  show:false},
      {  id:2,  img:'../image/dt/dianzan.png',  name:'ss',  biaozhi:'组员',  show:false},
      {  id:3,  img:'../image/dt/dianzan.png',  name:'hh',  biaozhi:'组员',  show:false},
      {  id:3,  img:'../image/dt/dianzan.png',  name:'hh',  biaozhi:'组员',  show:false},
      {  id:4,  img:'../image/dt/dianzan.png',  name:'ss',  biaozhi:'组员',  show:false}
    ],
    youmima:false,inputmima:'',listtimer:''
  },
  onLoad: function (options) {
    var that = this
    acid = options.id
    sitesite = options.site
    userinfo = wx.getStorageSync('headimg')
    openid = wx.getStorageSync('openid')
    wx.request({
      url: 'https://www.juquduo.com/juquduo/gaming/constructed/list',
      data:{
        activityId:acid
      },
      success(res){
        console.log(res)
        var list = res.data.Details
        if(list==null){
          wx.showModal({
            title:'暂无房间，请先创建房间',
            showCancel:false,
            success(res2){
              if (res2.confirm) {
                clearInterval(that.listtimer)
                wx.redirectTo({
                  url: './dacmode',
                })}}})
        }else{
          that.loadlist()
          that.listtimer = setInterval(function(){
            that.loadlist()
          },3000)
        }}
    })
  },

  loadlist(){
    var that = this
    wx.request({
      url: 'https://www.juquduo.com/juquduo/gaming/constructed/list',
      data:{
        activityId:acid
      },
      success(res){
        console.log(res)
        var temlist=[]
        var list = res.data.Details
        for(var i=0;i<list.length;i++){
          // 循环寻找对应房间号，如果满人或者没人，也要提示找不到房间
          // if(romid==list[i].RoomId&&list[i].Dynamic!='0'&&list[i].Dynamic!=list[i].RoomNumber){
            temlist.push(list[i])
          // }
        }
        that.setData({message1:temlist})
        if(temlist.length=='0'){
          wx.showModal({
            title:'暂无房间，请先创建房间',
            showCancel:false,
            success(res2){
              clearInterval(that.listtimer)
              if (res2.confirm) {
                wx.redirectTo({
                  url: './dacmode',
                })}}})
        }
      }
    })
  },
  onShow: function () {
    
  },
  onHide(){
    clearInterval(this.listtimer)
  },
  onUnload(){
    clearInterval(this.listtimer)
  },
  //无密码则进入房间 
  toroom(e){
    var that = this
    console.log(e.target.dataset.key)
    roommsg = e.target.dataset.key
    if(roommsg.RoomPwd!=''){
      this.setData({youmima:true})
    }else{
      wx.request({
        url: 'https://www.juquduo.com/juquduo/gaming/constructed/join',
        data:{
          roomId:roommsg.RoomId,
          activityId:acid,
          openid:openid,
          avatar:userinfo.avatarUrl,
          nick_name:userinfo.nickName
        },
        success(res){
          console.log(res)
          wx.redirectTo({
            url: './game?name='+roommsg.RoomName+'&renshu='+roommsg.RoomNumber+'&id='+roommsg.RoomId+'&acid='+acid+'&site='+sitesite,
          })
          clearInterval(that.listtimer)
        }
      })
    }
  },
  // 检查密码
  checkmima(e){
    var that = this
    console.log(e.detail.value)
    var mima = e.detail.value
    console.log(mima.length)
    if(mima == roommsg.RoomPwd){
      wx.request({
        url: 'https://www.juquduo.com/juquduo/gaming/constructed/join',
        data:{
          roomId:roommsg.RoomId,
          activityId:acid,
          openid:openid,
          avatar:userinfo.avatarUrl,
          nick_name:userinfo.nickName
        },
        success(res){
          console.log(res)
          wx.redirectTo({
            url: './game?name='+roommsg.RoomName+'&renshu='+roommsg.RoomNumber+'&id='+roommsg.RoomId+'&acid='+acid+'&site='+sitesite,
          })
          clearInterval(that.listtimer)
        }
      })
    }else if(mima.length=='6'&& mima != roommsg.RoomPwd){
      wx.showToast({
        icon:'none',
        title: '密码错误，请重新输入',
      })
      that.setData({inputmima:''})
    }
  },
  // 点空白隐藏输入框
  quxiaoshuru(){
    this.setData({youmima:false})
  },
  //复制房间号
  copybtn(e){
    console.log(e.target.dataset.key)
    var id = e.target.dataset.key
    wx.setClipboardData({
      data: id+'',
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              icon:'none',
              title: '已复制'
            })}})},
      fail(res){
        console.log(res)
      }
    })
  },
  //搜索房间号
  send(e){
    var that = this
    console.log(e.detail.value)
    romid = e.detail.value
    wx.request({
      url: 'https://www.juquduo.com/juquduo/gaming/constructed/list',
      data:{
        activityId:acid
      },
      success(res){
        console.log(res)
        var list = res.data.Details
        var temlist=[]
        // 如果输入框不为空
        if(romid!=''){
          for(var i=0;i<list.length;i++){
            // 循环寻找对应房间号，如果满人或者没人，也要提示找不到房间
            if(romid==list[i].RoomId&&list[i].Dynamic!='0'&&list[i].Dynamic!=list[i].RoomNumber){
              temlist.push(list[i])
            }
          }
          if(temlist.length=='0'){
            wx.showToast({
              icon:'none',
              title: '未搜索到房间,请重试',
            })
          }else{
            that.setData({message1:temlist})
          }
        // 输入框为空则显示全部
        }else{
          that.setData({message1:list})
        }
      }
    })
  }
})